<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2019/4/9
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;

class Area extends Base
{
    public $table = 'area';
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $data = $this->getAllArea();
        $this->assign('data',json_encode($data));
        return $this->fetch();
    }

    public function add()
    {
        if(request()->isPost()){
            return $this->save();
        }
        $this->assign('data',1);
        $this->assign('title','新增');
        $this->assign('url',url('add'));
        return $this->fetch('info');
    }

    public function update()
    {
        $id = input('id/d');
        $data = Db::name($this->table)->where('id',$id)->find();
        if(!$data){
            $this->error('不存在');
        }
        if(request()->isPost()){
            return $this->save();
        }

        $this->assign('data',json_encode($data));
        $this->assign('title','编辑');
        $this->assign('url',url('update'));
        return $this->fetch('info');
    }

    public function ajax_del_area()
    {
        $id = input('id');
        $data = Db::name($this->table)->where('pid',$id)->find();
        if($data){
            return dataReturn(0,'请先删除下级行政单位');
        }
        $res = Db::name($this->table)->where('id',$id)->delete();
        if($res)
            return dataReturn(1,'删除成功');
        return dataReturn(0,'删除失败');
    }



    protected function save()
    {
        $param = input();
        $data['name'] = input('name');
        if(!$data['name']){
            return dataReturn(0,'名称不能为空');
        }
        $data['sort'] = input('sort/d');
        $data['modtime'] = time();
        if($param['id']){
            //修改
            $res = Db::name($this->table)->where('id',$param['id'])->update($data);
        }else{
            //新增
            if($param['town_id']){
                $data['level'] = 5;
                $data['pid'] = $param['town_id'];
            }elseif ($param['county_id']){
                $data['level'] = 4;
                $data['pid'] = $param['county_id'];
            }elseif ($param['city_id']){
                $data['level'] = 3;
                $data['pid'] = $param['city_id'];
            }elseif ($param['province_id']){
                $data['level'] = 2;
                $data['pid'] = $param['province_id'];
            }else{
                $data['level'] = 1;
                $data['pid'] = 0;
            }
            $data['addtime'] = time();
            $res = Db::name($this->table)->insert($data);
        }
        if($res){
            return dataReturn(1,'保存成功');
        }
        return dataReturn(0,'保存失败');
    }

    protected function getAllArea()
    {
        $data = Db::name($this->table)->field('id,name as text,pid,level')->order('sort desc,id desc')->select();
        $level1 = $level2 = $level3 = $level4 = $level5 = [];
        foreach ($data as $value){
            switch ($value['level']){
                case 1:
                    $value['tags'][] = '省';
                    $level1[] = $value;
                    break;
                case 2:
                    $value['tags'][] = '市';
                    $level2[] = $value;
                    break;
                case 3:
                    $value['tags'][] = '县/区';
                    $level3[] = $value;
                    break;
                case 4:
                    $value['tags'][] = '乡/镇/街道';
                    $level4[] = $value;
                    break;
                case 5:
                    $value['tags'][] = '村/社区';
                    $level5[] = $value;
                    break;
            }
        }
        $level4 = $this->tree_node($level4,$level5);
        $level3 = $this->tree_node($level3,$level4);
        $level2 = $this->tree_node($level2,$level3);
        $level1 = $this->tree_node($level1,$level2);
        return $level1;
    }

    /**
     * @param $p_data 父级权限
     * @param $data 子级权限
     * @return mixed
     * 树级菜单权限
     */
    public function tree_node($p_data,$data)
    {
        foreach ($p_data as $key=>$value)
        {
            foreach ($data as $k=>$v)
            {
                if($value['id'] == $v['pid']){
                    $p_data[$key]['nodes'][] = $v;
                }
            }
        }
        return $p_data;
    }
}